<?php

/* $Id$ */

// echo json_encode($_SERVER);exit;

$my_sig_secret = md5('qiaolun@dragonzone.com/xmonad');

require_once 'osapi/osapi.php';
require_once 'signature_method.php';
require_once 'config_consumer.php';

$_fk_s = isset($_GET['_fk_s']) ? $_GET['_fk_s'] : '';

if($_fk_s == 'orkut.com'){


    //Build a request object from the current request
    $request = OAuthRequest::from_request(null, null, array_merge($_GET, $_POST));

    //Initialize the new signature method
    $signature_method = new OrkutSignatureMethod();

    //Check the request signature
    $signature_valid = $signature_method->check_signature($request, null, null, $_GET["oauth_signature"]);

}elseif($_fk_s == 'myspace.com'){

    $params = array_merge($_GET, $_POST);

    //Build a request object from the current request
    $request = OAuthRequest::from_request(null, null, $params);

    //Initialize the new signature method
    $signature_method = new MyspaceSignatureMethod();

    //Check the request signature
    $consumer_setting = ConfigConsumer::get($_fk_s);
    $signature_valid = $signature_method->check_signature($request, new OAuthConsumer($consumer_setting['key'],$consumer_setting['secret']), null, $_GET["oauth_signature"]);


} else {
    echo json_encode('bad source');exit;
}


//Build the output object
$payload = array();
//Add extra parameters to help debugging
$payload["query"] = array_merge($_GET, $_POST);
$payload["rawpost"] = file_get_contents("php://input");

if ($signature_valid == true) {
    $payload["validated"] = true;
} else {
    $payload["validated"] = false;
    //Return the response as JSON
    echo json_encode($payload);exit;

}

// do my jobs below

$params = array();
$params['sig_viewer_id'] = $_GET['opensocial_viewer_id'];
$params['sig_owner_id'] = $_GET['opensocial_owner_id'];
$params['sig'] = generate_sig($params, $my_sig_secret);

$payload['new_params'] = $params;

function generate_sig($arr, $secret){
    ksort($arr);
    $base_string = http_build_query($arr);
    return md5($base_string . $secret);
}



//Return the response as JSON
echo json_encode($payload);exit;


